import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { UserController } from './controller/user/user.controller';
import { UserService } from './service/user/user.service';
import { EmailController } from './controller/email/email.controller';
import { MailerModule } from '@nestjs-modules/mailer';
import { CodeGenerator } from './utils';
import { EmailService } from './service/email/email.service';
import { SessionInitMiddleware } from './midd/session-init.middleware';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './entity/user.entity';
import { QrcodeController } from './controller/qrcode/qrcode.controller';
import { TokenVerifyMiddleware } from './midd/token-verify-middleware.middleware';
import { QrCodeService } from './service/qrcode.service';
import { WebSocketServerGeteWay } from './ws/ws.server';

@Module({
  imports: [
    // 邮箱发送服务
    MailerModule.forRoot({
      transport: {
        host: "smtp.qq.com",
        port: "465",
        auth: {
          user: "1162280694@qq.com",
          pass: "nxbtexihtgqfbaga",
        }
      }
    }),

    // mysql服务
    TypeOrmModule.forRoot({
      type:"mysql",
      database:"ycc-im",
      host:"www.dmyily2.cn",
      port:3306,
      username:"ycc-im",
      password:"ycc-im123456",
      entities:['dist/entity/*.entity{.ts,.js}'], 
      synchronize: true,
      logging:true,
      charset: 'utf8mb4',
    }),
    TypeOrmModule.forFeature([
      User
    ])



  ],
  controllers: [
    UserController,
    EmailController,
    QrcodeController],
  providers: [UserService, EmailService,QrCodeService,WebSocketServerGeteWay],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
    // session init
    .apply(SessionInitMiddleware)
    .forRoutes("*")

    // token 检查
    .apply(TokenVerifyMiddleware)
    .exclude("/sv/login","/sv/sendEmail","/sv/qrCode","/sv/vaildScanCode","/ws")
    .forRoutes("*")

  }

 }
